package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.pc.model.CarStaticModel;
import com.sbl.bajw.pc.model.LogTModel;
import com.sbl.bajw.pc.service.CarStaticService;
import com.sbl.bajw.pc.service.LogTModelService;
import com.sbl.bajw.util.DateUtil;
import com.sbl.bajw.util.JsonUtil;

@Controller
@RequestMapping("/CarStatic")
public class CarStaticAction {

	@Resource
	private CarStaticService service;

	@Resource
	private LogTModelService logService;
	
	String[] referred = {"京","津","冀","晋","蒙","辽","吉","黑","沪","苏","浙","皖","闽","赣","鲁","豫","鄂","楚","湘","粤","桂","琼","渝","川","蜀","贵","黔","云","滇","藏","陕","秦","甘","陇","青","宁","新","港","澳","台"};
	
	/**
	 * @标题：createApp
	 * @作者：罗琦
	 * @时间：2015年5月11日
	 * @作用：静态车辆采集数据入库
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createApp")
	public @ResponseBody String createApp(HttpServletRequest request, HttpServletResponse response) throws Exception{
		CarStaticModel car = RequestUnit.getModel(CarStaticModel.class);
		
		//积分入库
		LogTModel logT = new LogTModel();
		logT.setUserid(car.getCreater());
		logT.setCode("11");
		
		if(car.getCarCode() != null && !"".equals(car.getCarCode())){
			int length = car.getCarCode().length();
			//讲字符串转为字符数组
			String strArray[] = new String[length];
			for(int j = 0; j < strArray.length; j++){
				strArray[j] = car.getCarCode().charAt(j) + "";
			}
			//判断车牌号码首字母是否是referred数组中的一个
			boolean in = Arrays.asList(referred).contains(strArray[0]);
			if(in){
				Pattern pattern = Pattern.compile("^[\u4e00-\u9fa5|WJ]{1}[A-Z0-9]{5}[\u4e00-\u9fa5|A-Z0-9]{1}$");
				Matcher matcher = pattern.matcher(car.getCarCode());
				if (!matcher.matches()) {
					car.setIsTrue("0");
				}else{
					car.setIsTrue("1");
				} 
			}else{
				car.setIsTrue("0");
			}
		}
		service.insert(car);
		logService.insert(logT);
		
		return JsonResultUtil.Success();
	}
	
	/**
	 * @标题：getList
	 * @作者：罗琦
	 * @时间：2015年5月11日
	 * @作用：后台列表展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getList")
	public @ResponseBody String getList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String json = service.getList(parm);
		return json;
	}
	
	/**
	 * @标题：download
	 * @作者：罗琦
	 * @时间：2015年5月12日
	 * @作用：静态车辆采集数据导出
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/download")
	public void download(HttpServletRequest request, HttpServletResponse response) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		List<CarStaticModel> list = service.downloadList(parm);
		for(int i = 0; i < list.size(); i++){
			if("0".equals(list.get(i).getMnc())){
				list.get(i).setMnc("中国移动");
			}else if("1".equals(list.get(i).getMnc())){
				list.get(i).setMnc("中国联通");
			}else if("2".equals(list.get(i).getMnc())){
				list.get(i).setMnc("中国电信");
			}
		}
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("rownum", "序号");
		column.put("lon", "经度");
		column.put("lat", "纬度");
		column.put("carCode", "车牌号码");
		column.put("carColor", "车牌颜色");
		column.put("autoCarCode", "原始识别车牌号码");
		column.put("autoCarColor", "演示识别车牌颜色");
		column.put("address", "采集地址");
		column.put("note", "备注");
		column.put("mnc", "移动网络类型");
		column.put("lac", "位置区域码");
		column.put("cid", "基站编号");
		column.put("bsss", "基站信号强度");
		column.put("showname", "采集人");
		column.put("createtime", "采集时间");
		column.put("uuid", "唯一标识码");
		HSSFWorkbook wb = ExcelUtil.createMExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=CarStatic.xls");  
		wb.write(outStream);
	}
	
	/**
	 * @标题：getModel
	 * @作者：罗琦
	 * @时间：2015年5月12日
	 * @作用：静态车辆采集详情
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detail")
	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		CarStaticModel car = service.getModel(id);
		List<HashMap<String, String>> pics = service.getPics(id);
		RequestUnit.setAttr("car", car);
		RequestUnit.setAttr("pics", pics);
		ModelAndView mav = new ModelAndView("/view/manager/car/detail");
		return mav;
	}
	
	@RequestMapping(value = "/List")
	public ModelAndView List(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mav = new ModelAndView("/view/manager/car/List");
		return mav;
	}
	
	/**
	 * @标题：getListApp
	 * @作者：罗琦
	 * @时间：2015年5月13日
	 * @作用：APP静态车辆采集列表查询
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getListApp")
	public @ResponseBody String getListApp(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		String start = DateUtil.getTodayDate();
		String stop = DateUtil.getYesterdayDate();
		parm.put("startdate", start + " 00:00:00");
		parm.put("stopdate", stop + "23:59:59");
		
		List<HashMap<String, String>> list = service.getListApp(parm);
//		String json = JsonResultUtil.Success(JsonUtil.ConvertArrayToJson(list));
		int num = service.getCountApp(parm);
		String result = "{\"num\":" + num + ",\"list\":" + JsonUtil.ConvertArrayToJson(list) + "}";
		String json = JsonResultUtil.Success("", JsonUtil.ConvertObjectToJson(result));
		return json;
	}
	
	/**
	 * @标题：detailApp
	 * @作者：罗琦
	 * @时间：2015年5月13日
	 * @作用：静态车辆信息APP展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detailApp")
	public @ResponseBody String detailApp(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("car_id");
		
		List<HashMap<String, String>> pics = service.getPics(id);
		CarStaticModel car = service.getModel(id);
		
		String result = "{\"image\":" + JsonUtil.ConvertArrayToJson(pics) + ",\"data\":" + JsonUtil.ConvertObjectToJson(car) + "}";
		return JsonResultUtil.Success("", result);
	}
	
	@RequestMapping(value = "/mapView")
	public ModelAndView mapView(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String carCode = RequestUnit.getString("carCode");
		List<CarStaticModel> points = service.getCarByCode(carCode);
		
		RequestUnit.setAttr("points", points);
		
		ModelAndView mav = new ModelAndView("/view/manager/car/mapView");
		return mav;
	}
	@RequestMapping(value = "/detailInMap")
	public ModelAndView detailInMap(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = RequestUnit.getString("id");
		CarStaticModel model = service.getModel(id);
		
		RequestUnit.setAttr("model", model);
		
		ModelAndView mav = new ModelAndView("/view/manager/car/detailInMap");
		return mav;
	}
	
	@RequestMapping(value = "/getCarPoints")
	public @ResponseBody String getCarPoints(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = RequestUnit.getString("id");
		
		List<CarStaticModel> points = service.getCarByCode(id);
		RequestUnit.setAttr("points", points);
		
		String result = JsonUtil.ConvertArrayToJson(points);
		return JsonResultUtil.Success(result);
	}
	
	/**
	 * @标题：update
	 * @作者：罗琦
	 * @时间：2015年6月15日
	 * @作用：后台标记更新
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/update")
	public @ResponseBody String update(HttpServletRequest request, HttpServletResponse response) throws Exception{
		CarStaticModel car = RequestUnit.getModel(CarStaticModel.class);
		car.setUpdater(SessionUnit.getUserId());
		service.update(car);
		return JsonResultUtil.Success();
	}
}
